package com.gitee.feizns.web.ex.impl;

import com.gitee.feizns.web.domain.result.Err;
import com.gitee.feizns.web.domain.result.Ret;
import com.gitee.feizns.web.ex.CustomizeExceptionHandler;
import com.gitee.feizns.web.ex.GlobalRestfulExceptionAdvice;
import com.gitee.feizns.web.ex.ServiceException;
import org.springframework.http.ResponseEntity;

/**
 * @author feizns
 * @since 2019/7/7
 */
public class ServiceExceptionHandler implements CustomizeExceptionHandler<ServiceException, Object> {

    @Override
    public ResponseEntity<Ret<Object>> handle(ServiceException e) {
        Err err = e.getErr();

        if ( err == null ) {
            ResponseEntity ret = GlobalRestfulExceptionAdvice.errorMappingHandle(e);
            if ( ret != null )
                return ret;
        }

        if ( err != null )
            return ResponseEntity.ok(Ret.err(e.getCode(), e.getMessage(), e.getData()));
        return ResponseEntity.ok(Ret.err(CustomizeExceptionHandler.defaultMsg(e)));
    }

}
